package com.okRPC.server.config;

import com.okRPC.discovery.DiscoveryService;
import com.okRPC.discovery.ZooKeeperDiscoveryService;
import com.okRPC.register.RegistryService;
import com.okRPC.register.ZooKeeperRegistryService;
import com.okRPC.server.RpcServerProvider;
import com.okRPC.server.transport.NettyRpcServer;
import com.okRPC.server.transport.RpcServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Description: This is a sample class.
 * Author: Simon
 */
@Configuration
@EnableConfigurationProperties(RpcServerProperties.class)
public class RpcServerAutoConfiguration {

    @Autowired
    private RpcServerProperties properties;

    @Bean
    @ConditionalOnMissingBean(RpcServer.class)
    public RpcServer rpcServer(){
        return new NettyRpcServer();
    }

    @Bean
    @ConditionalOnMissingBean(RegistryService.class)
    public RegistryService registryService(){
        return new ZooKeeperRegistryService(properties.getRegistryAddr());
    }

    @Bean
    @ConditionalOnMissingBean(RpcServerProvider.class)
    public RpcServerProvider rpcServerProvider(@Autowired RegistryService registryService,
                                               @Autowired RpcServer rpcServer,
                                               @Autowired RpcServerProperties rpcServerProperties){
        return new RpcServerProvider(registryService,rpcServerProperties,rpcServer);
    }
}
